package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.InterfaceCountDao;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.SetmealDao;
import com.itheima.utils.DateUtils;
import org.apache.ibatis.annotations.MapKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.*;

/**
 * 运营统计管理功能
 */
/*
*  reportDate:null,
   todayNewMember :0,
   totalMember :0,
   thisWeekNewMember :0,
   thisMonthNewMember :0,
   todayOrderNumber :0,
   todayVisitsNumber :0,
   thisWeekOrderNumber :0,
   thisWeekVisitsNumber :0,
   thisMonthOrderNumber :0,
   thisMonthVisitsNumber :0,
   hotSetmeal :[
       {name:'阳光爸妈升级肿瘤12项筛查（男女单人）体检套餐',setmeal_count:200,proportion:0.222},
       {name:'阳光爸妈升级肿瘤12项筛查体检套餐',setmeal_count:200,proportion:0.222}
   ]
* */
@Service(interfaceClass = ReportService.class)
@Transactional
public class ReportServiceImpl implements ReportService {

    @Autowired
    private MemberDao memberDao;

    @Autowired
    private OrderDao orderDao;
    @Autowired
    private SetmealDao setmealDao;

    @Autowired
    private JedisPool jedisPool;

    @Autowired
    private InterfaceCountDao interfaceCountDao;

    //运营数据统计查询
    @Override
    public Map<String, Object> findMemberAndOrder() throws Exception {
        Map<String,Object> data=new HashMap<>();
        //获取当前时间
        String date = DateUtils.parseDate2String(DateUtils.getToday());
        //获取本周星期一的月份日期
        String Monday = DateUtils.parseDate2String(DateUtils.getThisWeekMonday());
        //获取本月第一天的日期
        String month = DateUtils.parseDate2String(DateUtils.getFirstDay4ThisMonth());
//                 reportDate:null,
        //将时间添加到map集合中
          data.put("reportDate",date);
//                todayNewMember :0,
        //查询当天会员数量
        Integer todaymemberCount = memberDao.findMemberCountByDate(date);
        data.put("todayNewMember",todaymemberCount);
//                totalMember :0,
        //查询总会员数
        Integer MembertotalCount = memberDao.findMemberTotalCount();
        data.put("totalMember",MembertotalCount);
//                thisWeekNewMember :0,
        //查询本周新增会员数
        Integer thisWeekNewMember = memberDao.findMemberCountAfterDate(Monday);
        data.put("thisWeekNewMember",thisWeekNewMember);
//                thisMonthNewMember :0,
        //查询本月新增会员数
        Integer thisMonthNewMember = memberDao.findMemberCountAfterDate(month);
        data.put("thisMonthNewMember",thisMonthNewMember);
//                todayOrderNumber :0,
           //查询当天总预约数
        Integer todayOrderNumber = orderDao.findOrderCountByDate(date);
        data.put("todayOrderNumber",todayOrderNumber);
//                todayVisitsNumber :0,
        //查询今日到诊数
        Integer todayVisitsNumber = orderDao.findVisitsCountByDate(date);
        data.put("todayVisitsNumber",todayVisitsNumber);
//                thisWeekOrderNumber :0,
        //查询本周预约数
        Integer thisWeekOrderNumber = orderDao.findOrderCountAfterDate(Monday);
        data.put("thisWeekOrderNumber",thisWeekOrderNumber);
//                thisWeekVisitsNumber :0,
        //查询本周到诊数
        Integer thisWeekVisitsNumber = orderDao.findVisitsCountAfterDate(Monday);
        data.put("thisWeekVisitsNumber",thisWeekVisitsNumber);
//                thisMonthOrderNumber :0,
        //查询本月预约数
        Integer thisMonthOrderNumber = orderDao.findOrderCountAfterDate(month);
        data.put("thisMonthOrderNumber",thisMonthOrderNumber);
//                thisMonthVisitsNumber :0,
        //查询本月预约数
        Integer thisMonthVisitsNumber = orderDao.findVisitsCountAfterDate(month);
        data.put("thisMonthVisitsNumber",thisMonthVisitsNumber);
//        hotSetmeal :[
//        {name:'阳光爸妈升级肿瘤12项筛查（男女单人）体检套餐',setmeal_count:200,proportion:0.222},
//        {name:'阳光爸妈升级肿瘤12项筛查体检套餐',setmeal_count:200,proportion:0.222}
//   ]
        //查询热门套餐数据信息
        List<Map> hotSetmeal = orderDao.findHotSetmeal();
        data.put("hotSetmeal",hotSetmeal);
        return data;
    }

    @Override
    public List<Map<String, Object>> findIncomeByDate(String startDates,String endDates) {
        List<Map<String, Object>> incomes = new ArrayList<>();

        List<Map<Object,Object>> setmealPrices = setmealDao.findSetmealPriceAndId();
        List<Map<Object,Object>> setmealCount = orderDao.findSetmealCountByDate(startDates,endDates);

        for (Map<Object, Object> map1 : setmealPrices) {
            Integer id = (Integer) map1.get("id");
            Float price = (Float) map1.get("price");
            for (Map<Object, Object> map2 : setmealCount) {
                Integer setmeal_id = (Integer) map2.get("setmeal_id");
                Long count = (Long) map2.get("count");
                if (setmeal_id==id){
                    Map<String, Object> map = new HashMap<>();
                    map.put("value",count*price);
                    String name = setmealDao.findNameById(id);
                    map.put("name",name);
                    incomes.add(map);
                }
            }
        }
        return incomes;
    }




    //实现接口调用方法
    @Override
    public Map<String, Object> InterfaceReportData() {

        Jedis jedis = jedisPool.getResource();
        Set<String> mysortedset = jedis.zrange("interfaceCount", 0, -1);

        HashMap<String, Object> map = new HashMap<>();
        ArrayList<String> list = new ArrayList<>();
//        list.add("查询检查项");
//        list.add("查询检查组");


        ArrayList<Integer> integers = new ArrayList<>();
//        integers.add(1);
//        integers.add(2);

        if(mysortedset.size()!=0){
            for (String interfaceName : mysortedset) {

//                System.out.println(s+"-"+jedis.zscore("interfaceCount",s));
                list.add(interfaceName);
                integers.add((int)(jedis.zscore("interfaceCount",interfaceName).doubleValue()));
            }
        }
        map.put("interfaceNames",list);
        map.put("interfaceCount",integers);


        ArrayList<String> list1 = new ArrayList<>();
        String date = null;
        try {
            date = DateUtils.parseDate2String(DateUtils.getToday());
        } catch (Exception e) {
            e.printStackTrace();
        }
        list1.add(date);
        map.put("calendarDay",list1);
        return map;
    }

    @Override
    public Map<String, Object> InterfaceReportDataFromMysql(String date) {
        Map<String,Object> m = interfaceCountDao.countDate(date);
        Iterator it = m.keySet().iterator();
        // 键和值
        String key = null;
        Double value = null;
        while (it.hasNext()) {
            key = (String) it.next();if(key.equals("date" )||key.equals("id")){
                continue;
            }
            value = (Double) m.get(key);
            System.out.println("key:" + key + "---" + "value:" + value);

            jedisPool.getResource().zadd("interCount",value,key);
        }

        //以下返回数据
        Set<String> mysortedset = jedisPool.getResource().zrange("interCount", 0, -1);

        HashMap<String, Object> map = new HashMap<>();
        ArrayList<String> list = new ArrayList<>();


        ArrayList<Integer> integers = new ArrayList<>();


        if(mysortedset.size()!=0){
            for (String interfaceName : mysortedset) {

//                System.out.println(s+"-"+jedis.zscore("interfaceCount",s));
                list.add(interfaceName);
                integers.add((int)(jedisPool.getResource().zscore("interCount",interfaceName).doubleValue()));
            }
        }
        map.put("interfaceNames",list);
        map.put("interfaceCount",integers);


        ArrayList<String> list1 = new ArrayList<>();
        list1.add(date);
        map.put("calendarDay",list1);

        return map;

    }

    @Override
    public void setInterfaceReportData(HashMap<String, Object> map) {
        interfaceCountDao.setCountDate(map);
    }

}
